// 导入 createBrowserRouter
import { createBrowserRouter } from 'react-router-dom';
// 导入主布局
import MainLayout from '../components/MainLayout';
// 只导入已存在的后台管理页面
import UserManagement from '../pages/UserManagement'; // 用户权限管理
import OrganizationManagement from '../pages/OrganizationManagement'; // 组织架构管理
import DataManagement from '../pages/DataManagement'; // 数据管理与报表
import Login from '../pages/Login'; // 登录页面
import StockManagement from '../pages/StockManagement'; // 库存管理
import CategoryManagement from '../pages/CategoryManagement'; // 商品分类管理
import { getCurrentUser } from '../store/auth';

// 动态生成子路由
function getChildrenRoutes() {
  const user = getCurrentUser();
  if (user && user.role === 'admin') {
    return [
      { index: true, element: <UserManagement /> },
      { path: 'user-management', element: <UserManagement /> },
      { path: 'category-management', element: <CategoryManagement /> },
      { path: 'stock-management', element: <StockManagement /> },
      { path: 'organization-management', element: <OrganizationManagement /> },
      { path: 'data-management', element: <DataManagement /> },
    ];
  } else {
    return [
      { index: true, element: <UserManagement /> },
      { path: 'user-management', element: <UserManagement /> },
      { path: 'category-management', element: <CategoryManagement /> },
      { path: 'stock-management', element: <StockManagement /> },
      
    ];
  }
}

// 创建路由
const router = createBrowserRouter([
  {
    path: '/login', // 登录页
    element: <Login />,
  },
  {
    path: '/', // 根路径
    element: <MainLayout />, // 主布局
    children: getChildrenRoutes(),
  },
]);

// 导出路由
export default router; 